define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Exchangerecord, $timeout, GoPage) {
		
		//初始化页面
		$scope.init_data = function(){
			$scope.firstHide = false;//避免闪现"没有数据"
			Exchangerecord.getList(1,function(data){
				$scope.firstHide = true;
				console.log(data)
				if(!data) return;
				$scope.datas = data;
			})
		}
		$scope.init_data();
		
		/****************上下拉刷新****************/
		$scope.nPage = 1;
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Exchangerecord.getList(1, function(data) {
				$scope.datas = data;
				$scope.nPage = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Exchangerecord.getList($scope.nPage + 1, function(data) {
				if(data.data != null) {
					if($scope.datas.data != undefined) {
						$scope.datas.data = $scope.datas.data.concat(data.data);
						$scope.nPage += 1;
					} else {
						$scope.datas = data;
					}
				}
			},function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		/****************上下拉刷新****************/
		
		//跳转到兑换商品详情
		$scope.onshow_detail = function(goods_id){
			GoPage.go_states("pointgoods",{'id':goods_id});
		}
		
		
	}

	controller.$inject = ['$scope', 'Exchangerecord', '$timeout','GoPage'];

	return app.registerController("exchangerecordctrl",controller);
});